package com.google.code.sip.ucenter.web.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.core.utils.WebGridUtils;
import com.google.code.sip.ucenter.model.Credit;
import com.google.code.sip.ucenter.model.CreditType;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.CreditManager;
import com.google.code.sip.ucenter.service.CreditTypeManager;
import com.google.code.sip.ucenter.service.UserManager;

@Controller
public class CreditController {

	@Autowired
	private CreditManager creditManager;
	@Autowired
	private CreditTypeManager creditTypeManager;
	@Autowired
	private UserManager userManager;

	@RequestMapping("/admin/credit!list.do")
	public ModelAndView list(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>();
		// 获取用户page
		Page<User> page = new Page<User>();
		WebGridUtils.fillPage(request, page);
		// 查询条件
		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String huoshowId = request.getParameter("huoshowId");
		boolean hasUserName = StringUtils.isNotBlank(username);
		boolean hasEmail = StringUtils.isNotBlank(email);
		boolean hasHuoshowId = StringUtils.isNotBlank(huoshowId);

		if (hasUserName) {
			page.addFilter("username", username.trim());
			model.put("username", username.trim());
		}
		if (hasEmail) {
			page.addFilter("email", email.trim());
			model.put("email", email.trim());
		}
		if (hasHuoshowId) {
			page.addFilter("huoshowId", huoshowId.trim());
			model.put("huoshowId", huoshowId.trim());
		}

		userManager.findNormalUser(page);
		model.put("totalRows", (int) page.getTotalCount());
		model.put("page", page);

		// 获取积分类型
		List<CreditType> creditTypeList = creditTypeManager.getAllCreditTypes();
		model.put("creditTypeList", creditTypeList);

		// 用户积分map<用户名，积分列表>
		Map<String, List<Credit>> userCreditMap = new HashMap<String, List<Credit>>();
		// 用户总积分
		Map<String, Integer> userCountMap = new HashMap<String, Integer>();
		for (User user : page.getResult()) {
			List<Credit> creditList = creditManager.getCreditesByUser(user);
			userCreditMap.put(user.getUsername(), creditList);
			int count = userManager.getCreditCount(user);
			userCountMap.put(user.getUsername(), count);
		}
		model.put("userCreditMap", userCreditMap);
		model.put("userCountMap", userCountMap);
		return new ModelAndView("admin.credit.list", model);

	}

	@RequestMapping("/admin/credit!recalc.do")
	public ModelAndView recalc(HttpServletRequest request) {
		creditManager.reCalcUserCreditCount();
		return list(request);
	}
}
